package com.nulldev.util.analytics.v2;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.nulldev.util.java.swing.SwingUtil;
import com.nulldev.util.java.swing.SwingUtil.ScrollDirection;
import com.nulldev.util.java.swing.extSwing.lookandfeel.themes.ThemeApplier;
import com.nulldev.util.net.IPUtil;
import com.nulldev.util.os.BIOSUtil;
import com.nulldev.util.os.CPUUtil;
import com.nulldev.util.os.GPUUtil;
import com.nulldev.util.os.OSUtil;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

public class nAnalytics2PermissionPrompt extends JFrame {

	private JPanel contentPane;
	private JScrollPane scrollPane;

	/**
	 * Launch the application.
	 * @return 
	 */
	public static nAnalytics2PermissionPrompt main() {
		nAnalytics2PermissionPrompt frame = new nAnalytics2PermissionPrompt();
		ThemeApplier.applyTheme(frame);
		SwingUtil.scroll(frame.getScrollPane(), ScrollDirection.UP);
		frame.setVisible(true);		
		SwingUtil.centerFrame(frame);
		return frame;
	}

	/**
	 * Create the frame.
	 */
	public nAnalytics2PermissionPrompt() {
		setTitle("nAnalytics.v2.PermissionPrompt");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 715, 681);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JButton btnDeny = new JButton("Deny");
		btnDeny.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				nAnalytics2.DISABLED = true;
				setVisible(false);
				dispose();
			}
		});
		
		JButton btnAllow = new JButton("Allow");
		btnAllow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				nAnalytics2.DISABLED = false;
				setVisible(false);
				dispose();
			}
		});
		
		scrollPane = new JScrollPane();
		
		JButton btnConfigureDataAllowd = new JButton("Configure Data Allowed");
		btnConfigureDataAllowd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				nAnalytics2Configurator.toggle();
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(btnDeny, GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnAllow, GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(12)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
					.addGap(12))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(btnConfigureDataAllowd, GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(btnConfigureDataAllowd)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnDeny)
						.addComponent(btnAllow)))
		);
		
		JTextPane txtpnSampleDataCpu = new JTextPane();
		txtpnSampleDataCpu.setText("nAnalytics(v2) requires your personal data to continue."
				+ "\nYou may allow or deny data collection."
				+ "\nThis data can help improve software that is created."
				+ "\n\nOnce 'Allow' is pressed, we will collect the data sampled below, this may take a few seconds."
				+ "\n\nData we'll collect (If everything is checked):"
				+ "\nCPU: " + CPUUtil.parse().toString()
				+ "\nGPU: " + GPUUtil.parse().toString()
				+ "\nNetwork:"
				+ "\n - Local IP: 127.0.0.1"
				+ "\n - Global IP: " + IPUtil.getLocalHostLANAddress_safe()
				+ "\n - Internet IP: " + IPUtil.getPublicIP()
				+ "\nOS:"
				+ "\n - OS Type: " + (OSUtil.isWindows() ? OSUtil.Windows_Type.getType() : OSUtil.getOperatingSystemType())
				+ "\n - Detailed OS Information: " + OSUtil.getOperatingSystemString()
				+ "\nCrash Information:"
				+ "\n - Crashes: 0"
				+ "\n - Crash Data:"
				+ "\n   - "
				+ "\nBIOS Information:"
				+ "\n - " + BIOSUtil.parse().toString()
				
		);
		txtpnSampleDataCpu.setEditable(false);
		scrollPane.setViewportView(txtpnSampleDataCpu);
		contentPane.setLayout(gl_contentPane);
		SwingUtil.scroll(scrollPane, ScrollDirection.UP);
	}
	
	protected JScrollPane getScrollPane() {
		return scrollPane;
	}
}
